/**
 * Copyright ©2003~2019年3月28日 szkingdom
 * @author raohj
 * @date 2019年3月28日
 * @file RSerializable.java
 * @comment : please input your comment to this file
 * @version : input version
 * @modify:
 * 		1、 2019年3月28日 由 raohj 新建文件
 */
package com.ccnu.demojava.serializable;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Date;
import java.util.Random;

/**
 * @author raohj
 * @date 2019年3月28日
 * @version 
 * @comment to add your comment
 */
public class RSerializable {
	private static Random rd = new Random(new Date().getTime());
	
	public static void serializeble() {
		Integer d0 = rd.nextInt();
		Path createF = Paths.get("D:", "temp", "tmp", d0.toString());
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(Files.newOutputStream(createF, StandardOpenOption.CREATE));
			RA ra = new RA("raohaijun", 66);
			oos.writeObject(ra);
			
			System.out.println("序列化写入文件:" + createF.getFileName());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			if(oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		// 以下为反序列化
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(Files.newInputStream(createF));
			RA rb = (RA)ois.readObject();
			System.out.println(rb.toString());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
